package com.ciwong.epaper.modules.homework.ui;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.android.volley.TimeoutError;
import com.ciwong.epaper.R;
import com.ciwong.epaper.application.EApplication;
import com.ciwong.epaper.modules.epaper.dao.EpaperDao;
import com.ciwong.epaper.modules.homework.adapter.ClassWorkAdapter;
import com.ciwong.epaper.modules.homework.adapter.TeacherWorkAdapter;
import com.ciwong.epaper.modules.homework.bean.ClassWorkRecord;
import com.ciwong.epaper.modules.homework.bean.ClassWorkRecordsPageBean;
import com.ciwong.epaper.modules.homework.bean.PublishWorkRecords;
import com.ciwong.epaper.modules.me.bean.PublishWorkRecordsPageBean;
import com.ciwong.epaper.ui.BaseFragment;
import com.ciwong.epaper.util.BaseExtCallBack;
import com.ciwong.epaper.widget.PullToRefreshView;
import com.ciwong.libs.utils.NetworkUtils;
import com.ciwong.libs.widget.CWToast;
import com.ciwong.mobilelib.i.BaseCallBack;
import com.ciwong.mobilelib.utils.ToastUtil;
import com.ciwong.mobilelib.widget.listview.PullRefreshListView;

import java.util.List;

/**
 * @description: 课堂作业列表
 * @author: wyk
 * @time: 2021/10/9 14:20
 */
public class ClassWorkFragment extends BaseFragment implements PullToRefreshView.OnHeaderRefreshListener, PullToRefreshView.OnFooterRefreshListener, ExpandableListView.OnGroupClickListener,
        ExpandableListView.OnChildClickListener, View.OnClickListener{

    private RelativeLayout handle_go_to_bookdesk;

    private LinearLayout publish_work_lay;

    private PullToRefreshView my_work_refresh_view;

    private ExpandableListView my_work_exlistv;

    private ClassWorkAdapter classWorkAdapter;

    private List<ClassWorkRecord> classWorkRecords;

    private final int defult_page = 1;

    /**
     * 当前页码
     */
    private int curPage = defult_page;

    /**
     * 默认加载数据大小
     */
    private static final int LOAD_PAGE_SIZE = 20;



    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_class_work, container, false);
        initView(view);
        init();
        return view;
    }

    protected void initView(View view) {
        //tea_add_work_text = view.findViewById(R.id.tea_add_work_text);
        my_work_refresh_view = view.findViewById(R.id.my_work_refresh_view);
        my_work_exlistv = view.findViewById(R.id.my_work_exlistv);
        publish_work_lay = view.findViewById(R.id.publish_work_lay);
        handle_go_to_bookdesk = view.findViewById(R.id.handle_go_to_bookdesk);
        //dk_work_publish = view.findViewById(R.id.dk_work_publish);
        getClassWorkRecords(curPage);
    }

    private int curItem;

    protected void init() {
        classWorkAdapter = new ClassWorkAdapter(mActivity, classWorkRecords);
        my_work_refresh_view.setOnHeaderRefreshListener(this);
        my_work_refresh_view.setOnFooterRefreshListener(this);
        my_work_exlistv.setAdapter(classWorkAdapter);

        my_work_exlistv.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView expandableListView, View view, int i, long l) {
                return true;
            }
        });
    }

    /**
     * 获取课堂作业列表
     * @param page
     */
    private void getClassWorkRecords(int page){

        EpaperDao.getInstance().getClassWorkRecords(EApplication.BRAND_ID,
                page, LOAD_PAGE_SIZE, new BaseCallBack() {
                    @Override
                    public void failed(int errorCode, Object data) {
                        hideCricleProgress();
                        my_work_refresh_view.onFooterRefreshComplete();
                        my_work_refresh_view.onHeaderRefreshComplete();
                        failed(data);
                    }

                    @Override
                    public void failed(Object data) {
                        hideCricleProgress();
                        my_work_refresh_view.onFooterRefreshComplete();
                        my_work_refresh_view.onHeaderRefreshComplete();
                        if (data instanceof TimeoutError) {
                            hideCricleProgress();
                            ToastUtil.INSTANCE.toastCenterError(getString(R.string.load_faild_check_net_work));
                        }
                    }

                    @Override
                    public void success(Object data) {
                        hideCricleProgress();
                        my_work_refresh_view.onFooterRefreshComplete();
                        my_work_refresh_view.onHeaderRefreshComplete();
                        if (null != data) {
                            ClassWorkRecordsPageBean classWorkRecordsPageBean = (ClassWorkRecordsPageBean) data;
                            setWorkList(classWorkRecordsPageBean);
                        }
                    }
                });
    }

    /**
     * 设置列表数据
     * @param publishWorkRecordsPageBean
     */
    private void setWorkList(ClassWorkRecordsPageBean publishWorkRecordsPageBean) {
        try {
            List<ClassWorkRecord> pageList = publishWorkRecordsPageBean.getPageList();
            int totalCount = publishWorkRecordsPageBean.getTotalCount();
            if (0 == totalCount) {
                handle_go_to_bookdesk.setVisibility(View.VISIBLE);
                publish_work_lay.setVisibility(View.GONE);
            } else {
                classWorkRecords = pageList;
                handle_go_to_bookdesk.setVisibility(View.GONE);
                publish_work_lay.setVisibility(View.VISIBLE);
                if (curPage == 1) {
                    classWorkAdapter.clear();
                    init();
                } else {
                    classWorkAdapter.addAll(pageList);
                    //my_work_exlistv.setSelectionFromTop(firstPostion,top);
                }
                for (int i = 0; i < classWorkAdapter.getGroupCount(); i++) {
                    my_work_exlistv.expandGroup(i);
                }

                if (classWorkAdapter.getGroupCount() == publishWorkRecordsPageBean.getTotalCount()) {
                    my_work_refresh_view.setFooterState(PullToRefreshView.PULL_TO_NO_MORE);
                } else {
                    my_work_refresh_view.setFooterState(PullToRefreshView.PULL_TO_REFRESH);
                }
            }

        }catch (Exception e){

        }
    }


    @Override
    public void onClick(View view) {

    }

    @Override
    public boolean onChildClick(ExpandableListView expandableListView, View view, int i, int i1, long l) {
        return false;
    }

    @Override
    public boolean onGroupClick(ExpandableListView expandableListView, View view, int i, long l) {
        return false;
    }

    @Override
    public void onFooterRefresh(PullToRefreshView view) {
        if (!NetworkUtils.isOnline()) {
            CWToast.makeText(mActivity, getString(R.string.connect_disable), Toast.LENGTH_LONG).setToastType(CWToast.TOAST_TYPE_ERROR).show();
            return;
        }
        curPage++;
        getClassWorkRecords(curPage);
    }

    @Override
    public void onHeaderRefresh(PullToRefreshView view) {
        curPage = 1;
        getClassWorkRecords(curPage);
    }
}
